| Conditions | 1 | 
| Paths | > 20000 | 
| Total Lines | 339 | 
| Code Lines | 247 | 
| Lines | 154 | 
| Ratio | 45.43 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 1 | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var TableHelperSingleton = (function(){  | 
            ||
| 3 |     function initInstance() { | 
            ||
| 4 |         return { | 
            ||
| 5 |             BuildRequest: { | 
            ||
| 6 | View Code Duplication |                 Sort: function (rq, strDesc){ | 
            |
| 
                                                                                                    
                        
                         | 
                |||
| 7 |                     function sortBySpan(span, i){ | 
            ||
| 8 | var order = span.innerHTML;  | 
            ||
| 9 |                         if(order.length === 1){ | 
            ||
| 10 | rq.colNo = i;  | 
            ||
| 11 | rq.colOrd = order === strDesc ? "desc" : "asc";  | 
            ||
| 12 | }  | 
            ||
| 13 | return rq.colNo === i;  | 
            ||
| 14 | }  | 
            ||
| 15 | |||
| 16 | var thTags = document.getElementById(rq.tableId)  | 
            ||
| 17 |                             .getElementsByTagName("thead")[0] | 
            ||
| 18 |                             .getElementsByTagName("th"); | 
            ||
| 19 | var length = thTags.length;  | 
            ||
| 20 |                     for(var i = 0; i < length; i++){ | 
            ||
| 21 |                         var link = thTags[i].getElementsByTagName("a")[0]; | 
            ||
| 22 |                         if(link){ | 
            ||
| 23 |                             var span = link.getElementsByTagName("span")[0]; | 
            ||
| 24 |                             if(span && sortBySpan(span, i)){ | 
            ||
| 25 | break;  | 
            ||
| 26 | }  | 
            ||
| 27 | }  | 
            ||
| 28 | }  | 
            ||
| 29 | },  | 
            ||
| 30 | View Code Duplication |                 Filter: function (rq){ | 
            |
| 31 |                     function getFilterFieldsByTableID(tableID){ | 
            ||
| 32 |                         var fields = {filterBy: null, filter: null}; | 
            ||
| 33 | var filterDiv = getFilterDivByTableIDOrNull(tableID);  | 
            ||
| 34 |                         if(filterDiv !== null){ | 
            ||
| 35 | setFilterBy(fields, filterDiv);  | 
            ||
| 36 | setFilterValue(fields, filterDiv);  | 
            ||
| 37 | }  | 
            ||
| 38 | return fields;  | 
            ||
| 39 | }  | 
            ||
| 40 |                     function getFilterDivByTableIDOrNull(tableID){ | 
            ||
| 41 | var res = null;  | 
            ||
| 42 |                         if(document.getElementById(tableID).parentNode.getElementsByTagName("div").length > 0){ | 
            ||
| 43 |                             for(var i = 0; i < document.getElementById(tableID).parentNode.getElementsByTagName("div").length; i++){ | 
            ||
| 44 |                                 if(document.getElementById(tableID).parentNode.getElementsByTagName("div")[i].getAttribute("class") === "filter"){ | 
            ||
| 45 |                                     return document.getElementById(tableID).parentNode.getElementsByTagName("div")[i]; | 
            ||
| 46 | }  | 
            ||
| 47 | }  | 
            ||
| 48 | |||
| 49 | }  | 
            ||
| 50 | return res;  | 
            ||
| 51 | }  | 
            ||
| 52 |                     function setFilterBy(fields, filterDiv){ | 
            ||
| 53 |                         var slctObj = filterDiv.getElementsByTagName("select")[0]; | 
            ||
| 54 |                         if(slctObj && slctObj.options[slctObj.selectedIndex].value !== "all"){ | 
            ||
| 55 | fields.filterBy = slctObj.options[slctObj.selectedIndex].value;  | 
            ||
| 56 | }  | 
            ||
| 57 | }  | 
            ||
| 58 |                     function setFilterValue(fields, filterDiv){ | 
            ||
| 59 |                         var textObj = filterDiv.getElementsByTagName("input")[0]; | 
            ||
| 60 |                         if(textObj && textObj.value && textObj.value.length !== 0){ | 
            ||
| 61 | fields.filter = encodeURIComponent(textObj.value.trim());  | 
            ||
| 62 | }  | 
            ||
| 63 | }  | 
            ||
| 64 | |||
| 65 | var r = getFilterFieldsByTableID(rq.tableId);  | 
            ||
| 66 |                     if(r.filter !== null){ | 
            ||
| 67 | rq.filter = r.filter;  | 
            ||
| 68 | }  | 
            ||
| 69 |                     if(r.filterBy !== null){ | 
            ||
| 70 | rq.filterBy = r.filterBy;  | 
            ||
| 71 | }  | 
            ||
| 72 | },  | 
            ||
| 73 |                 Run: (function(rq, crntTableId, strDesc){ | 
            ||
| 74 | rq.tableId = crntTableId;  | 
            ||
| 75 | th_instance.BuildRequest.Sort(rq, strDesc);  | 
            ||
| 76 | th_instance.BuildRequest.Filter(rq);  | 
            ||
| 77 | })  | 
            ||
| 78 | },  | 
            ||
| 79 | View Code Duplication |             ColumnHover: function(tableContainer, index){ | 
            |
| 80 | var rows = document.getElementById(tableContainer).rows;  | 
            ||
| 81 | var upto = rows.length - 1;  | 
            ||
| 82 |                 if(typeof index === "undefined"){ | 
            ||
| 83 | ColumnHoverRelease(rows, upto);  | 
            ||
| 84 |                 } else { | 
            ||
| 85 |                     for(var i = 0; i < upto; i++){ | 
            ||
| 86 |                         rows[i].cells[index].setAttribute("lang", "col-hover"); | 
            ||
| 87 | }  | 
            ||
| 88 | }  | 
            ||
| 89 |                 function ColumnHoverRelease(rows, upto){ | 
            ||
| 90 |                     for(var i = 0; i < upto; i++){ | 
            ||
| 91 |                         for(var j = 0; j < rows[i].cells.length; j++){ | 
            ||
| 92 |                             if(rows[i].cells[j].lang){ | 
            ||
| 93 |                                 rows[i].cells[j].removeAttribute("lang"); | 
            ||
| 94 | }  | 
            ||
| 95 | }  | 
            ||
| 96 | }  | 
            ||
| 97 | };  | 
            ||
| 98 | },  | 
            ||
| 99 |             Draw: { | 
            ||
| 100 | View Code Duplication |                 Section: function(tableContainer, dt, tSection){ | 
            |
| 101 | var section = tSection === "tfoot" ? "tfoot" : "tbody";  | 
            ||
| 102 | var tSec = document.getElementById(tableContainer)  | 
            ||
| 103 | .getElementsByTagName(section)[0];  | 
            ||
| 104 | Clear(tSec, th_instance.IePrior(9));  | 
            ||
| 105 |                     for(var i = 0; i < dt.length; i++){ | 
            ||
| 106 | var row = dt[i];  | 
            ||
| 107 |                         var tRow = document.createElement("tr"); | 
            ||
| 108 | Row(row, tRow);  | 
            ||
| 109 | tSec.appendChild(tRow);  | 
            ||
| 110 |                         if(section === "tfoot"){ | 
            ||
| 111 | th_instance.ProcessPaginationLinks(tSec);  | 
            ||
| 112 | }  | 
            ||
| 113 | }  | 
            ||
| 114 |                     function Clear(tSection, iePrior9){ | 
            ||
| 115 |                         if(iePrior9){ | 
            ||
| 116 |                             if(tSection.firstChild){ | 
            ||
| 117 |                                 while(tSection.firstChild){ | 
            ||
| 118 | tSection.removeChild(tSection.firstChild);  | 
            ||
| 119 | }  | 
            ||
| 120 | }  | 
            ||
| 121 |                         }else{ | 
            ||
| 122 | tSection.innerHTML = "";  | 
            ||
| 123 | }  | 
            ||
| 124 | }  | 
            ||
| 125 |                     function Row(row, tRow){ | 
            ||
| 126 |                         for(var cell in row){ | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 127 |                             var tCell = document.createElement("td"); | 
            ||
| 128 | if(typeof row[cell] === "string" ||  | 
            ||
| 129 | typeof row[cell] === "number"  | 
            ||
| 130 |                             ){ | 
            ||
| 131 | tCell.innerHTML = row[cell];  | 
            ||
| 132 |                             }else if(typeof row[cell] === "object"){ | 
            ||
| 133 | RowCellFromObject(row, cell, tCell);  | 
            ||
| 134 | }  | 
            ||
| 135 | tRow.appendChild(tCell);  | 
            ||
| 136 | }  | 
            ||
| 137 | }  | 
            ||
| 138 |                     function RowCellFromObject(row, cell, tCell){ | 
            ||
| 139 |                         for(var attr in row[cell]){ | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 140 |                             if(typeof row[cell][attr] === "string"){ | 
            ||
| 141 | tCell.innerHTML = row[cell][attr];  | 
            ||
| 142 |                             }else if(typeof row[cell][attr] === "object"){ | 
            ||
| 143 |                                 for(var v in row[cell][attr]){ | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 144 | tCell.setAttribute(v, row[cell][attr][v]);  | 
            ||
| 145 | }  | 
            ||
| 146 | }  | 
            ||
| 147 | }  | 
            ||
| 148 | }  | 
            ||
| 149 | },  | 
            ||
| 150 |                 Run: function(tableContainer, d, instance){ | 
            ||
| 151 | this.Section(tableContainer, d.body);  | 
            ||
| 152 | this.Section(tableContainer, d.footer, "tfoot");  | 
            ||
| 153 |                     if(instance.rq !== null){ | 
            ||
| 154 | var hover = document.getElementById(instance.rq.tableId)  | 
            ||
| 155 |                                     .getElementsByTagName("th")[instance.rq.colNo].lang; | 
            ||
| 156 |                         if(hover){ | 
            ||
| 157 | instance.ColumnHover(tableContainer, instance.rq.colNo);  | 
            ||
| 158 | }  | 
            ||
| 159 | }  | 
            ||
| 160 | }  | 
            ||
| 161 | },  | 
            ||
| 162 |             Export: function(lnk, eType, strDesc){ | 
            ||
| 163 |                 var request = {}; | 
            ||
| 164 |                 var crntTableId = th_instance.GetParent(lnk, "table").getAttribute("id"); | 
            ||
| 165 | th_instance.BuildRequest.Run(request, crntTableId, strDesc);  | 
            ||
| 166 | request.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ?  | 
            ||
| 167 | eType :  | 
            ||
| 168 | "csv";  | 
            ||
| 169 | window.open(th_instance.RequestToUrl(request));  | 
            ||
| 170 | return false;  | 
            ||
| 171 | },  | 
            ||
| 172 |             Filter: { | 
            ||
| 173 |                 Run: function(field, instance, rq, strDesc){ | 
            ||
| 174 | var crntTableId = th_instance.Filter.GetTableId(field);  | 
            ||
| 175 |                     if(crntTableId !== null){ | 
            ||
| 176 |                         var request = {}; | 
            ||
| 177 | var exRq = rq;  | 
            ||
| 178 | th_instance.BuildRequest.Run(request, crntTableId, strDesc);  | 
            ||
| 179 | if(exRq === null ||  | 
            ||
| 180 | request.filter !== exRq.filter ||  | 
            ||
| 181 | request.filterBy !== exRq.filterBy  | 
            ||
| 182 |                         ){ | 
            ||
| 183 | th_instance.LoadData.Run(crntTableId, request, instance);  | 
            ||
| 184 | }  | 
            ||
| 185 | }  | 
            ||
| 186 | },  | 
            ||
| 187 |                 GetTableId: function(field){ | 
            ||
| 188 |                     if(field.tagName.toLowerCase() !== "select"){ | 
            ||
| 189 |                         return field.getAttribute("data-table-id"); | 
            ||
| 190 | }  | 
            ||
| 191 |                     var f = field.parentNode.parentNode.getElementsByTagName("input")[0]; | 
            ||
| 192 |                     return '' === f.value ? null : f.getAttribute("data-table-id"); | 
            ||
| 193 | }  | 
            ||
| 194 | },  | 
            ||
| 195 |             Init: { | 
            ||
| 196 |                 Run: function(tableId){ | 
            ||
| 197 | var tContainer = document.getElementById(tableId);  | 
            ||
| 198 |                     if(!th_instance.IePrior(9)){ | 
            ||
| 199 | th_instance.Init.SetColumnsHoverEffect(tContainer, tableId);  | 
            ||
| 200 | }  | 
            ||
| 201 |                     var tfoot = tContainer.getElementsByTagName("tfoot")[0]; | 
            ||
| 202 | th_instance.ProcessPaginationLinks(tfoot);  | 
            ||
| 203 | },  | 
            ||
| 204 |                 SetColumnsHoverEffect: function (tContainer, tableId){ | 
            ||
| 205 | var tHcells = tContainer.rows[0].cells;  | 
            ||
| 206 |                     for(var i = 0; i < tHcells.length; i++){ | 
            ||
| 207 |                         if(tHcells[i].firstChild.tagName === "A"){ | 
            ||
| 208 |                             tHcells[i].firstChild.setAttribute("onmouseover", "table.ColumnHover('" + tableId + "'," + i + ");"); | 
            ||
| 209 |                             tHcells[i].firstChild.setAttribute("onmouseout", "table.ColumnHover('" + tableId + "');"); | 
            ||
| 210 | }  | 
            ||
| 211 | }  | 
            ||
| 212 | }  | 
            ||
| 213 | },  | 
            ||
| 214 |             GoPage: { | 
            ||
| 215 |                 Run: function(lnk, instance, strDesc){ | 
            ||
| 216 |                     var request = {}; | 
            ||
| 217 |                     var crntTableId = th_instance.GetParent(lnk, "table").getAttribute("id"); | 
            ||
| 218 | th_instance.BuildRequest.Run(request, crntTableId, strDesc);  | 
            ||
| 219 | request.pageNo = th_instance.GoPage.GetNo(lnk, crntTableId);  | 
            ||
| 220 | th_instance.LoadData.Run(crntTableId, request, instance);  | 
            ||
| 221 | return false;  | 
            ||
| 222 | },  | 
            ||
| 223 |                 GetNo: function(lnk, tableId){ | 
            ||
| 224 | //check & serve pagination jump links  | 
            ||
| 225 | var jumpDir = lnk.innerHTML.trim().substr(0, 1);  | 
            ||
| 226 |                     if(jumpDir === "+" || jumpDir === "-"){ | 
            ||
| 227 | var current = document.getElementById(tableId)  | 
            ||
| 228 |                                         .querySelector("tfoot .paging .a").innerHTML; | 
            ||
| 229 |                         var jump = lnk.innerHTML.replace("K", "000").replace("M", "000000000"); | 
            ||
| 230 | var jumpPage = (parseInt(current) + parseInt(jump));  | 
            ||
| 231 |                         lnk.parentNode.setAttribute("data-page", jumpPage); | 
            ||
| 232 | lnk.style.transform = "none";  | 
            ||
| 233 | }  | 
            ||
| 234 |                     return lnk.parentNode.hasAttribute("data-page") ? | 
            ||
| 235 |                             lnk.parentNode.getAttribute("data-page") : | 
            ||
| 236 | lnk.innerHTML;  | 
            ||
| 237 | }  | 
            ||
| 238 | },  | 
            ||
| 239 |             LoadData: { | 
            ||
| 240 | tail: null,  | 
            ||
| 241 |                 Run: function(tableContainer, rq, instance){ | 
            ||
| 242 | var tail = th_instance.LoadData.tail;  | 
            ||
| 243 |                     if(tail!==null){ tail.abort();} | 
            ||
| 244 | th_instance.LoadData.SetVisability(tableContainer, false);  | 
            ||
| 245 | var xmlhttp = window.XMLHttpRequest ?  | 
            ||
| 246 | new XMLHttpRequest() : //IE7+, Firefox, Chrome, Opera, Safari  | 
            ||
| 247 |                                     new window.ActiveXObject("Microsoft.XMLHTTP");//IE6, IE5 | 
            ||
| 248 |                     xmlhttp.onreadystatechange = function(){ | 
            ||
| 249 |                         if(xmlhttp.readyState === 4 && xmlhttp.status === 200){ | 
            ||
| 250 | var d = JSON.parse(xmlhttp.responseText);  | 
            ||
| 251 | th_instance.Draw.Run(tableContainer, d, instance);  | 
            ||
| 252 | th_instance.LoadData.SetVisability(tableContainer, true);  | 
            ||
| 253 | instance.LoadEndCalback(tableContainer);  | 
            ||
| 254 | }  | 
            ||
| 255 | };  | 
            ||
| 256 |                     xmlhttp.open("GET", th_instance.RequestToUrl(rq), true); | 
            ||
| 257 | xmlhttp.send();  | 
            ||
| 258 | th_instance.LoadData.tail = xmlhttp; //put at tail to can abort later previous request  | 
            ||
| 259 | },  | 
            ||
| 260 |                 SetVisability: function(tableContainer, flag){ | 
            ||
| 261 | var tbl = document.getElementById(tableContainer);  | 
            ||
| 262 |                     if(flag === true){ | 
            ||
| 263 | tbl.style.filter = "none";  | 
            ||
| 264 | tbl.style.opacity = "1";  | 
            ||
| 265 | tbl.style.cursor = "auto";  | 
            ||
| 266 |                     }else if(flag === false){ | 
            ||
| 267 | tbl.style.filter = "blur(1px)";  | 
            ||
| 268 | tbl.style.opacity = "0.8";  | 
            ||
| 269 | tbl.style.cursor = "wait";  | 
            ||
| 270 |                     }else{ | 
            ||
| 271 |                         console.error("table error in the flag value"); | 
            ||
| 272 | }  | 
            ||
| 273 | }  | 
            ||
| 274 | },  | 
            ||
| 275 |             Sort: function(colNo, lnk, instance, strDesc, strAsc){ | 
            ||
| 276 |                 var request = {}; | 
            ||
| 277 |                 var crntTableId = th_instance.GetParent(lnk, "table").getAttribute("id"); | 
            ||
| 278 | th_instance.BuildRequest.Run(request, crntTableId, strDesc);  | 
            ||
| 279 |                 if(Math.round(colNo) === request.colNo){ | 
            ||
| 280 | request.colOrd = (request.colOrd === "asc" ? "desc" : "asc");  | 
            ||
| 281 |                 }else{ | 
            ||
| 282 | request.colNo = Math.round(colNo);  | 
            ||
| 283 | request.colOrd = "asc";  | 
            ||
| 284 | }  | 
            ||
| 285 | th_instance.LoadData.Run(crntTableId, request, instance);  | 
            ||
| 286 | /* Clear and add new sort arrow */  | 
            ||
| 287 |                 var headSpans = th_instance.GetParent(lnk, "thead").getElementsByTagName("span"); | 
            ||
| 288 | var length = headSpans.length;  | 
            ||
| 289 |                 for(var i = 0; i < length; i++){ | 
            ||
| 290 | headSpans[i].innerHTML = "";  | 
            ||
| 291 | }  | 
            ||
| 292 |                 lnk.getElementsByTagName("span")[0].innerHTML = (request.colOrd === "desc" ? strDesc : strAsc); | 
            ||
| 293 | },  | 
            ||
| 294 | |||
| 295 |             IePrior: function(v){ | 
            ||
| 296 | var rv = false;  | 
            ||
| 297 |                 if(window.navigator.appName === 'Microsoft Internet Explorer'){ | 
            ||
| 298 | var ua = window.navigator.userAgent;  | 
            ||
| 299 |                     var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); | 
            ||
| 300 |                     if(re.exec(ua) !== null){ | 
            ||
| 301 | rv = parseFloat(RegExp.$1);  | 
            ||
| 302 | }  | 
            ||
| 303 | rv = rv < v ? true : false;  | 
            ||
| 304 | }  | 
            ||
| 305 | return rv;  | 
            ||
| 306 | },  | 
            ||
| 307 |             GetParent: function(obj, objType){ | 
            ||
| 308 |                 while(obj && obj.tagName !== objType.toUpperCase()){ | 
            ||
| 309 | obj = obj.parentNode;  | 
            ||
| 310 | }  | 
            ||
| 311 | return obj;  | 
            ||
| 312 | },  | 
            ||
| 313 |             ProcessPaginationLinks: function(tfoot){ | 
            ||
| 314 |                 var pLinks = tfoot.querySelectorAll(".paging a"); | 
            ||
| 315 |                 if(pLinks.length > 0){ | 
            ||
| 316 |                     for(var j = 0; j < pLinks.length; j++){ | 
            ||
| 317 |                         pLinks[j].setAttribute("href", "javascript:void(0);"); | 
            ||
| 318 |                         pLinks[j].setAttribute("onclick", "return table.GoPage(this);"); | 
            ||
| 319 | }  | 
            ||
| 320 | }  | 
            ||
| 321 | },  | 
            ||
| 322 | View Code Duplication |             RequestToUrl: function(rq){ | 
            |
| 323 | var url = location.pathname + ".json" + location.search;  | 
            ||
| 324 |                 if(typeof rq === "object"){ | 
            ||
| 325 |                     var getUrlVarName = { | 
            ||
| 326 | colNo: "col", colOrd: "ord", filter: "filter",  | 
            ||
| 327 | filterBy: "filter-by", pageNo: "pg", exportType: "export",  | 
            ||
| 328 | tableId: "table-id"  | 
            ||
| 329 | };  | 
            ||
| 330 | var flagFirst = location.search.length < 1 ? true : false;  | 
            ||
| 331 |                     for(var r in rq){ | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 332 | var clue = flagFirst === true ? "?" : "&";  | 
            ||
| 333 | url += clue + getUrlVarName[r] + "=" + rq[r];  | 
            ||
| 334 | flagFirst = false;  | 
            ||
| 335 | }  | 
            ||
| 336 | }  | 
            ||
| 337 | return url;  | 
            ||
| 338 | }  | 
            ||
| 339 | |||
| 340 | };  | 
            ||
| 341 | }  | 
            ||
| 342 |     return { | 
            ||
| 353 | var table_helper = TableHelperSingleton.getInstance();  |